<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Session Helper
*
* Functions related to sessions
* 
* @author EAE
* @link /helpers/
*/ 

    /**
    * Creates the session authentication for frontend users
    *
    * @access public
    * @param $user_id (string)
    * @param $user_nickname (string)
    * @param $user_level (string)
    * @param $user_email (string) 
    * @return void
    */
 
    function session_create($user_data = array())
    {

        $CI =& get_instance();
        
        $CI->session->set_userdata('log_in',TRUE);
        $CI->session->set_userdata('session_salt','2f25d52996726af871b5d6931e2d82fa');
        $CI->session->set_userdata('user_id',$user_data['id']); 
        $CI->session->set_userdata('user_name',$user_data['username']);

    }

    /**
    * Delete the session authentication for frontend users
    *
    * @access public
    * @return void
    */
 
    function session_delete()
    {
        $CI =& get_instance();
        
        $CI->session->unset_userdata('log_in');
        $CI->session->unset_userdata('session_salt');
        $CI->session->unset_userdata('user_id');
        $CI->session->unset_userdata('user_name'); 
    }




    function admin_session_create($user_data = array())
    {
        $CI =& get_instance();
        
        $CI->session->set_userdata('admin_log_in',TRUE);
        $CI->session->set_userdata('admin_session_salt','acfec6f05928053decffd8f8f85dfadf');
        $CI->session->set_userdata('admin_id',$user_data['id']); 
        $CI->session->set_userdata('admin_username',$user_data['Username']);
        $CI->session->set_userdata('admin_role',$user_data['Role']);
        
        //create session for image manager
        session_start();
        $_SESSION['MCImageManagerIsLoggedIn'] = TRUE;

    }                                          
    function admin_session_delete()
    {
        $CI =& get_instance();
        
        $CI->session->unset_userdata('admin_log_in');
        $CI->session->unset_userdata('admin_session_salt');
        $CI->session->unset_userdata('admin_id');
        $CI->session->unset_userdata('admin_username'); 
        $CI->session->unset_userdata('admin_role'); 

        //end session for image manager
        session_start();
        if(isset($_SESSION['MCImageManagerIsLoggedIn']))
            unset($_SESSION['MCImageManagerIsLoggedIn']); 

    }
    //session_start();
    //print $_SESSION['MCImageManagerIsLoggedIn'];


	/**
	* Check if user login is on and redirects to $url
	*
	* @access public
	* @param $url (char)
	* @return void
	*/
    /*
	function session_control($url,$status)
	{
		$CI =& get_instance();
		
		//check if session flag is TRUE - USER LOGGED
		
		switch($status)
		{
			
			case TRUE:
			if ($CI->session->userdata('log_in') && $CI->session->userdata('session_salt')=="2f25d52996726af871b5d6931e2d82fa")
			redirect($url, 'location');
			break;
			
			case FALSE:
			if (!$CI->session->userdata('log_in') && $CI->session->userdata('session_salt')!="2f25d52996726af871b5d6931e2d82fa")
			redirect($url, 'location');
			break;
			
		}

	}
    */
	
	/**
	* Check if user logged and returns bool
	*
	* @access public
	* @param $url (char)
	* @return bool
	*/
 
	function session_check()
	{
		$CI =& get_instance();
		
		//check if session flag is TRUE - USER LOGGED
		if ($CI->session->userdata('log_in') && $CI->session->userdata('session_salt')=='2f25d52996726af871b5d6931e2d82fa')
		return TRUE;
		else 
		return FALSE;

	}
	

	function admin_session_check()
	{
		$CI =& get_instance();

		//check if session flag is TRUE - USER LOGGED
		if ($CI->session->userdata('admin_log_in') && $CI->session->userdata('admin_session_salt')=='acfec6f05928053decffd8f8f85dfadf')
		return TRUE;
		else 
		return FALSE;
	
	}
	
	
	
	
	/**
	* Check if user login is on and redirects to $url
	*
	* @access public
	* @param $url (char)
	* @return void
	*/
    /*
	function admin_session_control($url,$status)
	{
		$CI =& get_instance();
		
		//check if session flag is TRUE - USER LOGGED
		
		switch($status)
		{
			
			case TRUE:
			if ($CI->session->userdata('admin_login') && $CI->session->userdata('session_salt_admin')=='acfec6f05928053decffd8f8f85dfadf')
			redirect($url, 'location');
			break;
			
			case FALSE:
			if (!$CI->session->userdata('admin_login') && $CI->session->userdata('session_salt_admin')!=='acfec6f05928053decffd8f8f85dfadf')
			redirect($url, 'location');
			break;
			
		}

	}
    */
    /**
    * checks if the logged in user has a role in the $allowed_roles array.
    * 
    * @access public
    * @param allowed_roles 
    * @return boolean 
    */
        
    if ( ! function_exists('is_allowed'))
    {        
        function is_allowed($allowed_roles)
        {
            $CI =& get_instance();
            
            if (!$CI->session->userdata('admin_log_in'))
                return FALSE;
            
            if ($allowed_roles == 'all') return TRUE;

            foreach($allowed_roles as $role)
            {
                if($CI->session->userdata('admin_role') == $role)
                    return TRUE;
            }
            return FALSE;
        }
    }

    /**
    * Generate the token, insert it in the session and returns it
    *
    * @access public
    * @param $form_name (string) -> name of the token to be stored in the session
    * @param $minutes (int) -> minutes the token is valid  
    * @return token (string)
    */

    function generate_token($form_name, $minutes = 2){
        $CI =& get_instance();
        
        $token = sha1(uniqid(rand(), true));
        $time = time() + ($minutes * 60); 
        $hash_data = array("token" => $token,
                            "time" => $time);   

        $CI->session->set_userdata($form_name, $hash_data);
        return $token;
    }

    /**
    * Return the token
    *
    * @access public
    * @param $form_name (string) -> name of the token stored in the session
    * @return token (string)
    */

    function get_token($form_name){
        $CI =& get_instance();
        $hash_data = $CI->session->userdata($form_name);

        if($hash_data === FALSE)
            return FALSE;

        if($hash_data["time"] < time()){
            $CI->session->unset_userdata($form_name);
            return FALSE;
        }

        return $hash_data["token"];
    }

    /**
    * Checks if the token submited is valid
    *
    * @access public
    * @param $form_name (string) -> name of the token stored in the session
    * @param $token (string) -> the token submited  
    * @return boolean
    */

    function validate_token($form_name, $token){
        $session_token = get_token($form_name);

        if($session_token === FALSE)
            return FALSE;

        if($session_token === $token)
            return TRUE;
        else
            return FALSE;

    }  


if ( ! function_exists('get_filter'))
{   
    function get_filter($key){
        $CI =& get_instance();

        //$CI->session->unset_userdata('admin_live_filter');    
        $hash_data = $CI->session->userdata('admin_live_filter');

        if($hash_data === FALSE)
            return FALSE;

        foreach($hash_data as $filter){
            if ($filter["key"] == $key) return $filter["value"];
        }
                                                         
        return FALSE;
    }
}

if ( ! function_exists('set_filter'))
{   
    function set_filter($key=NULL, $value=FALSE){
        $CI =& get_instance();

        $new_filter = array("key" => $key, "value" => $value);   
        $hash_data = $CI->session->userdata('admin_live_filter');

        if($hash_data === FALSE){

            $hash_data = array($new_filter);

        } else if (is_array($hash_data)){

            foreach($hash_data as &$filter){
                if ($filter["key"] === $key){
                    $filter["value"] = $value;
                    $found_key = true;
                }
            }

            if(!isset($found_key)){
                $hash_data[] = $new_filter;
            }

        }  
        $CI->session->set_userdata('admin_live_filter', $hash_data);     

    }
}


    /**
    * Generate the response and store it
    *
    * @access public
    * @param $form_name (string) -> name of the token to be stored in the session
    * @param $minutes (int) -> minutes the token is valid  
    * @return token (string)
    */
if ( ! function_exists('set_response'))
{ 
    function set_response($class, $msg){
       
        //print("<h1>set response:".$class." =>".$msg."</h1>");
        
        $CI =& get_instance();
          
        $hash_data = array(
                    'class' => $class,
                    'msg' => $msg
                    );
        
        //$CI->session->set_userdata("response", $hash_data);
        $CI->session->set_flashdata('response', $hash_data);
        //print("<h1>get responser: ".$CI->session->flashdata('responsexx')."</h1>");
        //die();
                          
        return;
    }
} 

    /**
    * Return the response
    *
    * @access public
    * @param $form_name (string) -> name of the token stored in the session
    * @return token (string)
    */
if ( ! function_exists('get_response'))
{ 
    function get_response(){
        $CI =& get_instance();  

        $hash_data = $CI->session->flashdata('response');//$CI->session->userdata("response");

        if($hash_data === FALSE)
            return FALSE;

        //$CI->session->unset_userdata("response");
        return $hash_data;
    }
};






 /**
    * Return the referring page before login
    *
    * @access public
    * @param 
    * @return string
    */ 
    function get_referrer()
    {
        $CI =& get_instance();  
        if ($CI->session->userdata('referrer'))
            return $CI->session->userdata('referrer');
        else
            return "/about/";
    }

    /**
    * Register the referring page as session var
    *
    * @access public
    * @param 
    * @return string
    */ 
    function set_referrer()
    {
        $CI =& get_instance(); 
        $CI->session->set_userdata('referrer', current_url());
    }
  

/* End of file sessions_helper.php */
/* Location: ./system/application/helpers/sessions_helper.php */ 